package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.NoticeEntity;
import com.example.springboot.system.mapper.NoticeMapper;
import com.example.springboot.system.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/noticeEntity")
public class NoticeController {


    private final NoticeService service;
    private final NoticeMapper mapper;

    public NoticeController(NoticeService service, NoticeMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/noticeList")
    public Result<?> noticeList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/noticeId")
    public Result<?> noticeId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/noticeListPage")
    public Result<?> noticeListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<NoticeEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<NoticeEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class Notice {
        private String name;
        private String content;
        private String type;
        private Integer userId;
        private Date current;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/noticeAppend")
    public Result<?> noticeAppend(@RequestBody NoticeController.Notice notice) {
        NoticeEntity entity = new NoticeEntity();
        BeanUtils.copyProperties(notice, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/noticeDelete")
    public Result<?> noticeDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/noticeEditor")
    public Result<?> noticeEditor(@RequestBody NoticeEntity noticeEntity) {
        QueryWrapper<NoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", noticeEntity.getId());
        return Result.success(service.updateById(noticeEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/noticeSearch")
    public Result<?> noticeSearch(@RequestParam String name) {
        QueryWrapper<NoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }


}
